<template>
    <!-- form 查询表单 -->
    <el-form :model="peisong" ref="formRef" label-width="auto" label-position="right" style="padding-bottom: 10px;">
        <el-row :gutter="20" style="margin-left: 40px">
            <el-col :span="8">
                <el-form-item prop="psid">
                    <el-input v-model="peisong.psid" placeholder="请输入配送id" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="6">
                <el-button type="primary" @click="doList">查询</el-button>
                <el-button type="default" @click="resetForm(formRef)">重置</el-button>
                <el-button type="success" @click="showAddDialog">新增</el-button>
            </el-col>
        </el-row>
    </el-form>

    <!-- table 表格 -->
    <el-table
        ref="deliveryTable"
        :data="peisongList"
        stripe
        style="width: 100%;"
        flexible
    >
        <el-table-column type="index" label="序号" min-width="100" />
        <el-table-column prop="psid" label="配送id" min-width="100"/>
        <el-table-column prop="ddid" label="订单id" min-width="100"/>
        <el-table-column prop="qsid" label="骑手id" min-width="100"/>
        <el-table-column prop="psks" label="配送开始时间" min-width="120"/>
        <el-table-column prop="psjs" label="配送结束时间" min-width="120"/>
        <el-table-column prop="pszt" label="配送状态" min-width="100">
            <template #default="scope">
                <el-tag v-if="scope.row.pszt === 0" type="info">未出餐</el-tag>
                <el-tag v-else-if="scope.row.pszt === 3" type="danger">已出餐</el-tag>
                <el-tag v-else-if="scope.row.pszt === 6" type="warning">配送中</el-tag>
                <el-tag v-else type="success">配送完成</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="300" >
            <template #default="scope">
                <el-button type="primary" @click="showDetailPage(scope.row)">详情</el-button>
                <el-button type="warning" @click="showUpdateDialog(scope.row)">更新</el-button>
                <el-button type="danger" @click="doDelete(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 新增 dialog-->
    <el-dialog v-model="addDialogVisible" title="新增配送信息" width="40%" top="2vh" destroy-on-close>
        <add-pei-song
            @flushPage="doFlushPage"
            @closeDialog="addDialogVisible = false">
        </add-pei-song>
    </el-dialog>

    <!-- 更新 dialog-->
    <el-dialog v-model="updateDialogVisible" title="更新配送信息" width="40%" top="2vh" destroy-on-close>
        <update-pei-song ref="updatePageRef"
                     @flushPage="doFlushPage"
                     @closeDialog="updateDialogVisible = false">
        </update-pei-song>
    </el-dialog>

    <!-- 详情 页面 -->
    <detail-pei-song ref="detailPageRef"></detail-pei-song>
</template>

<script setup lang="ts">
import {nextTick, onMounted, reactive, ref} from 'vue'
import {ElMessage, FormInstance} from "element-plus";
import axios from 'axios'
import AddPeiSong from "./AddPeiSong.vue";
import UpdatePeiSong from "./UpdatePeiSong.vue";
import DetailPeiSong from "./DetailPeiSong.vue";

const formRef = ref<FormInstance>();
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

// form 查询表单绑定的数据对象：peisong
const peisong = reactive({
    psid: '',
});

// table 数据表格绑定的数据对象：tableData
const peisongList = ref([]);
// 查询
const doList = async () => {
    peisongList.value = [];
    const url = peisong.psid ? `/api/peisong/${peisong.psid}` : '/api/peisong';
    const {data} = await axios.get(url)
    console.log(data);
    if(data.code === 200){
        peisongList.value = peisong.psid
            ? [data.data]
            : data.data;
        ElMessage.success('查询成功');
    }else{
        ElMessage.error(data.message);
    }
}

// 删除
const doDelete = async (obj) => {
    const {data} = await axios.delete(`/api/peisong`,{data:obj})
    console.log(data);
    if(data.code === 200){
        ElMessage.success('删除成功');
        await doList();
    }else{
        ElMessage.error(data.message);
    }
}

// 刷新页面
const doFlushPage = ()=>{
    resetForm(formRef.value);
    doList();
}

// 初始化
onMounted(()=>{
    doList();   // 查询数据
})
//--------------------------- 新增 对话框 ---------------------------
const addDialogVisible = ref(false);
const showAddDialog = ()=>{
    addDialogVisible.value = true;
}

//--------------------------- 更新 对话框 ---------------------------
const updateDialogVisible = ref(false);
const updatePageRef = ref(); // 组件引用
const showUpdateDialog = (obj)=>{
    updateDialogVisible.value = true;
    nextTick(()=>{
        updatePageRef.value.handleUpdateData(obj);
    })
}

//--------------------------- 详显 页面---------------------------
// 详显组件引用 -> 绑定到
const detailPageRef = ref();
const showDetailPage = (obj)=>{
    detailPageRef.value.handleDetailData(obj);
}
</script>